package cn.jhc.sevlet;//添加成绩

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.jhc.bean.Clazz;
import cn.jhc.bean.Course;
import cn.jhc.bean.Score;
import cn.jhc.bean.Student;
import cn.jhc.dao.ClassDao;
import cn.jhc.dao.CourseDao;
import cn.jhc.dao.ScoreDao;
import cn.jhc.dao.StudentDao;
import cn.jhc.dao.impl.ClassDaoImpl;
import cn.jhc.dao.impl.CourseDaoImpl;
import cn.jhc.dao.impl.ScoreDaoImp;
import cn.jhc.dao.impl.StudentDaoImpl;

/**
 * Servlet implementation class AddscoreServlet
 */
@WebServlet("/user/teacher/addscore.do")
public class AddscoreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddscoreServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.sendRedirect("addscore.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		String sno = request.getParameter("sno");
		StudentDao studentDao = new StudentDaoImpl();
		Student student = studentDao.getStudent(sno);
		String coursename = request.getParameter("course");
		CourseDao courseDao = new CourseDaoImpl();
		List<Course> courses = courseDao.list();
		Course course = null;
		boolean flag;
		try{
			for(int i=0;i<=courses.size();i++){
				if(courses.get(i).getCoursename().equals(coursename)){
					course = courses.get(i);
					break;
				}
			}
		
			BigDecimal uscore = new BigDecimal(request.getParameter("uscore"));
			BigDecimal endscore = new BigDecimal(request.getParameter("endscore"));
			BigDecimal totalscore = new BigDecimal(request.getParameter("totalscore"));
			String levelscore = request.getParameter("levelscore");
			String tname = request.getParameter("tname");
			Score score = new Score();
			score.setStudent(student);
			score.setUscore(uscore);
			score.setEndscore(endscore);
			score.setTotalscore(totalscore);
			score.setLevelscore(levelscore);
			score.setCourse(course);
			System.out.println("save!!!!!!!!!!!!!!!!!!!!!");
			ScoreDao scoreDao = new ScoreDaoImp();

			scoreDao.save(score);
			flag = true;
		} catch(Exception e){
			flag = false;
		}
		
		if(flag){
			response.sendRedirect("./tscore.do");
		}
		else{
			request.setAttribute("flag", false);
			request.getRequestDispatcher("addscore.jsp").forward(request,
					response);
		}
	}

}
